Skip to content

Comments

评估N-API迁移可行性与优势#1

Open
canvascat wants to merge 1 commit intomainfrom
cursor/n-api-a154
Open

评估N-API迁移可行性与优势#1
canvascat wants to merge 1 commit intomainfrom
cursor/n-api-a154

Conversation

@canvascat
Copy link
Owner

Migrate native addon from NAN to NAPI to improve stability, performance, and maintainability.

NAPI offers ABI stability, ensuring compatibility across Node.js versions without recompilation. It also provides better performance due to more direct V8 engine access, and a more modern, type-safe API for improved developer experience and long-term maintenance. A detailed migration guide (MIGRATION_TO_NAPI.md) has been added.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: NAPI Functions Fail Argument Validation

The NAPI functions Bind, Connect, and Close lack argument validation. They access args array elements (e.g., args[0], args[1]) without checking the actual number of arguments (argc) provided by napi_get_cb_info. This can lead to out-of-bounds access, undefined behavior, and crashes if fewer arguments than expected are passed from JavaScript.

src/abstract-socket-napi.cc#L48-L147

napi_value Bind(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
int32_t fd;
napi_get_value_int32(env, args[0], &fd);
size_t path_len;
napi_get_value_string_utf8(env, args[1], nullptr, 0, &path_len);
char* path = new char[path_len + 1];
napi_get_value_string_utf8(env, args[1], path, path_len + 1, nullptr);
int err = 0;
if (path[0] != '\0') {
err = -EINVAL;
goto out;
}
if (path_len > sizeof(sockaddr_un::sun_path)) {
err = -EINVAL;
goto out;
}
sockaddr_un s;
memset(&s, 0, sizeof(s));
memcpy(s.sun_path, path, path_len);
s.sun_family = AF_UNIX;
if (bind(fd, reinterpret_cast<sockaddr*>(&s), sizeof(s)) != 0) {
err = -errno;
}
out:
delete[] path;
napi_value result;
napi_create_int32(env, err, &result);
return result;
}
napi_value Connect(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
int32_t fd;
napi_get_value_int32(env, args[0], &fd);
size_t path_len;
napi_get_value_string_utf8(env, args[1], nullptr, 0, &path_len);
char* path = new char[path_len + 1];
napi_get_value_string_utf8(env, args[1], path, path_len + 1, nullptr);
int err = 0;
if (path[0] != '\0') {
err = -EINVAL;
goto out;
}
if (path_len > sizeof(sockaddr_un::sun_path)) {
err = -EINVAL;
goto out;
}
sockaddr_un s;
memset(&s, 0, sizeof(s));
memcpy(s.sun_path, path, path_len);
s.sun_family = AF_UNIX;
if (connect(fd, reinterpret_cast<sockaddr*>(&s), sizeof(s)) != 0) {
err = -errno;
}
out:
delete[] path;
napi_value result;
napi_create_int32(env, err, &result);
return result;
}
napi_value Close(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
int32_t fd;
napi_get_value_int32(env, args[0], &fd);
int err = 0;
if (close(fd)) {
err = -errno;
}
napi_value result;
napi_create_int32(env, err, &result);
return result;
}

Fix in CursorFix in Web


BugBot free trial expires on July 25, 2025
Learn more in the Cursor dashboard.

Was this report helpful? Give feedback by reacting with 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants